package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.enums.bpa.BpaProcessDetailTypeEnum;
import top.infopub.enums.cap.CapProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.mgr.bpa.dao.BpaContractBpaReviewDao;
import top.infopub.mgr.bpa.domain.BpaProjectDomain;
import top.infopub.mgr.bpa.domain.ContractNegDomain;
import top.infopub.mgr.bpa.jsonVo.ContractReviewJsonVo;
import top.infopub.mgr.bpa.service.BpaContractBpaReviewService;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.vo.ContractPublicVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.model.bpa.*;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Description:
 * @Author: Tszzzzzz
 * @Date: 2021/6/6
 */
@Service
public class BpaContractBpaReviewServiceImpl implements BpaContractBpaReviewService {

    private static final String type = "1";
    @Autowired
    private BpaContractBpaReviewDao bpaContractBpaReviewDao;
    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;
    @Override
    public Page<ContractNegDomain> selectByDomainWhere(Page<ContractNegDomain> page, ContractPublicVo contractPublicVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(contractPublicVo.getDates()))
        {
            String[] splitTime = contractPublicVo.getDates().split("~");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            contractPublicVo.setStartDate(format.parse(splitTime[0]));
            contractPublicVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<ContractNegDomain>) bpaContractBpaReviewDao.selectByDomain(contractPublicVo);
    }

    @Override
    public ContractReviewJsonVo queryDetailById(String id) {
        //评审主表信息
        BpaContractBpaReview bpaContractBpaReview = bpaContractBpaReviewDao.selectById(id);
        //基本信息
        BpaProjectDomain bpaProjectDomain = bpaContractBpaReviewDao.selectByDomain1(bpaContractBpaReview.getProCode());
        //合同评审信息
        BpaContractReviewPublic reviewPublic = bpaContractBpaReviewDao.selectReviewPublic(id,type);
        //合同清单汇总
        List<BpaContractSummary>  summary = bpaContractBpaReviewDao.selectSummary(id,type);
        //合同清单详细信息
        List<BpaContractListing> listing = bpaContractBpaReviewDao.selectListing(id,type);
        //商务指标信息
        BpaContractBusiness business = bpaContractBpaReviewDao.selectBusiness(id,type);
        //商务公式信息
        List<BpaContractFormula> formula = bpaContractBpaReviewDao.selectFormula(id,type);
        //商务投标信息
        BpaContractBid bid = bpaContractBpaReviewDao.selectBid(id,type);
        //附件信息
        List<ProjectAttachment> attachmentList = projectAttachmentDao.queryByWhere(bpaContractBpaReview.getProCode(), ProjectFileTypeEnum.FILE_TYPE63.getCode(), null);
        ContractReviewJsonVo contractReviewJsonVo = new ContractReviewJsonVo();
        contractReviewJsonVo.setProjectInfo(bpaProjectDomain);
        contractReviewJsonVo.setBpaContractBpaReview(bpaContractBpaReview);
        contractReviewJsonVo.setBpaContractReviewPublic(reviewPublic);
        contractReviewJsonVo.setBpaContractSummary(summary);
        contractReviewJsonVo.setBpaContractListing(listing);
        contractReviewJsonVo.setBpaContractBusiness(business);
        contractReviewJsonVo.setBpaContractFormula(formula);
        contractReviewJsonVo.setBpaContractBid(bid);
        contractReviewJsonVo.setFileAtt(fileAttaListToArray(attachmentList));
        return contractReviewJsonVo;
    }

    @Override
    @Transactional
    public RspVo saveAll(AuthorizationVO userDtl, String bodyStr, Integer submitType, BpaContractBpaReview contractReview, BpaContractReviewPublic bpaContractReviewPublic, List<BpaContractSummary> bpaContractSummary, List<BpaContractListing> bpaContractListing, List<BpaContractFormula> bpaContractFormula,BpaContractBid bpaContractBid, BpaContractBusiness bpaContractBusiness, List<ProjectAttachment> attachmentList) {
        //处理主表信息
        if(StringUtils.isNotBlank(contractReview.getId())){
            bpaContractBpaReviewDao.updateReview(contractReview);
        }else{
            bpaContractBpaReviewDao.insertReview(contractReview);
        }

        //处理合同评审公共信息
        bpaContractBpaReviewDao.deletePublic(contractReview.getId(),type);
        bpaContractReviewPublic.setId(IdFactory.getUUIDUpper());
        bpaContractReviewPublic.setContractId(contractReview.getId());
        bpaContractReviewPublic.setType(type);
        bpaContractBpaReviewDao.insertPublic(bpaContractReviewPublic);

        //处理合同清单信息
        if (bpaContractSummary.size()>0) {
            bpaContractBpaReviewDao.deleteSummary(contractReview.getId(),type);
        int seq = 0;
            for (BpaContractSummary summary : bpaContractSummary) {
                summary.setId(IdFactory.getUUIDUpper());
                summary.setContractId(contractReview.getId());
                summary.setType(type);
                summary.setSeq(seq++);
            }
            bpaContractBpaReviewDao.insertBatchSummary(bpaContractSummary);
        }


        //处理商务指标信息
        bpaContractBpaReviewDao.deleteBusiness(contractReview.getId(),type);
        bpaContractBusiness.setId(IdFactory.getUUIDUpper());
        bpaContractBusiness.setContractId(contractReview.getId());
        bpaContractBusiness.setType(type);
        bpaContractBpaReviewDao.insertBusiness(bpaContractBusiness);

        //处商务公示信息
        if (bpaContractFormula.size()>0) {
            bpaContractBpaReviewDao.deleteFormula(contractReview.getId(), type);
            for (BpaContractFormula formula : bpaContractFormula) {
                formula.setId(IdFactory.getUUIDUpper());
                formula.setContractId(contractReview.getId());
                formula.setType(type);
            }
            bpaContractBpaReviewDao.insertBatchFormula(bpaContractFormula);
        }

        //处理商务投标信息
        bpaContractBpaReviewDao.deleteBid(contractReview.getId(),type);
            bpaContractBid.setId(IdFactory.getUUIDUpper());
            bpaContractBid.setContractId(contractReview.getId());
            bpaContractBid.setType(type);
        bpaContractBpaReviewDao.insertBid(bpaContractBid);

        if (attachmentList!=null && attachmentList.size()>0){
            //批量插入附件信息
            projectAttachmentDao.deleteByWhere(contractReview.getProCode(), ProjectFileTypeEnum.FILE_TYPE63.getCode(), null);
            for (ProjectAttachment attachment : attachmentList) {
                attachment.setProCode(contractReview.getProCode());
            }
            projectAttachmentDao.insertBatch(attachmentList);
        }

        if (submitType==2){
            bpaProcessManageService.sendProcess(BpaProcessDetailTypeEnum.PRO_NODE_TYPE2, bodyStr, contractReview.getId(), contractReview.getProCode(), userDtl);
        }
        return RspVo.getSuccessResponse();

    }

    @Override
    @Transactional
    public RspVo saveListing(List<BpaContractListing> bpaContractListings, String id) {
        //处理合同清单详细信息
        int seq2 = 0;
        bpaContractBpaReviewDao.deleteListing(id,type);
        for (BpaContractListing listing : bpaContractListings) {
            listing.setContractId(id);
            listing.setType(type);
            listing.setSeq(seq2++);
        }
        bpaContractBpaReviewDao.insertBatchListing(bpaContractListings);
        return RspVo.getSuccessResponse();
    }

    public String[] fileAttaListToArray(List<ProjectAttachment> list) {
        if (list == null || list.size() <= 0) return null;
        String[] attaArr = new String[list.size()];
        int index = 0;
        for (ProjectAttachment pa : list) {
            StringBuffer tmp = new StringBuffer();
            tmp.append(pa.getSubTypes());
            tmp.append("|");
            tmp.append(pa.getFileName());
            tmp.append(pa.getFileSuffix());
            tmp.append("|");
            tmp.append(IdFactory.getUUIDSerialNumber());
            tmp.append("|");
            tmp.append(pa.getFilePath());
            attaArr[index] = tmp.toString();
            index++;
        }
        return attaArr;
    }
}
